<template>
	<view class="container">

		<view class="background">
			<view class="home">
				<u-search inputAlign="center" placeholder="请输入关键词搜索" :searchIconStyle="searchIconStyle" shape="round"
					v-model="keyword" maxlength="32" :actionStyle="actionStyle" @custom="custom" @search="search"
					@change="change"></u-search>
			</view>
		</view>
		<view>
			<view class="u-flex u-row-between home">
				<view class="title-blod">历史搜索</view>
				<view style="margin-right: 24upx;" @click="deleteAll">
					<image src="../../static/images/delete2.png" style="width: 32upx;height: 32upx;" mode=""></image>
				</view>
			</view>
			<view style="display: flex;flex-direction: row;align-items: center;">
				<!-- <u-tag :text="item" type="warning" mode="plain" class="tag" shape="circle" v-for="(item,index) in searchContentList" :key="index"></u-tag> -->

				<!-- <view style="border-radius: 25upx;border: 1px solid #b1b1b1;padding: 5upx 10upx;font-size: 25upx;">测试</view> -->
				<view style="border-radius: 25upx;border: 1px solid #b1b1b1;color: #b1b1b1;
			padding: 5upx 10upx;font-size: 25upx;margin-left: 24upx;" v-for="(item,index) in searchContentList" :key="index"
					@click="clickTag(item)">{{item}}</view>


			</view>
		</view>
		<view
			style="width: 90%;height: 1px;background-color: #c1c1c1;margin-left: 50%;transform: translate(-50%);margin-top: 20upx;">
		</view>
		<!-- 热搜榜 -->
		<view class="u-p-t-30">
			<view class="u-flex u-row-between home">
				<view style="display: flex;flex-direction: row;align-items: center;">
					<view class="title-blod">热门搜索</view>
					<image src="../../static/images/hot-img.png" style="width: 36upx;height: 36upx;margin-left: 20upx;"
						mode=""></image>

				</view>
				<!-- <view>
					<u-icon name="eye-fill" size="32"></u-icon>
				</view> -->
			</view>

			<view style="display: flex;flex-direction: row;align-items: center;">

				<view style="border-radius: 25upx;border: 1px solid #ff3131;color: #ff3131;
				padding: 5upx 10upx;font-size: 25upx;margin-left: 24upx;" v-for="(item,index) in hotList" :key="index"
					@click="clickTagHot(item)">{{item}}</view>

			</view>
		</view>

		<!-- top s -->
		<!-- 		<u-back-top :scrollTop="scrollTop" mode="circle" bottom="200" right="40" top="600" :icon="icon"
			:icon-style="iconStyle" :tips="tips" :custom-style="customStyle">
		</u-back-top> -->


		<view style="display: flex;flex-direction: column;">
			<view style="display: flex;flex-direction: row;margin-left: 24upx;margin-top: 20upx;
				background-color: #ffffff;margin-right: 24upx;" v-for="(item,index) in resourceList" :key="index"
				@click="toDetail(item.id)">
				<image :src="item.cover"
					style="width: 400upx;height: 200upx;border-radius: 20upx;box-shadow: 2upx  2upx 2upx #ababab;"
					mode=""></image>

				<view style="display: flex;flex-direction: column;margin-left: 24upx;width: 100%;">
					<view style="font-size: 28upx;font-weight: 500;color: #252526;margin-top: 5upx;">{{item.title}}
					</view>
					<view style="font-size: 25upx;color: #A6A6A6;margin-top: 10upx;">{{item.title}}</view>

					<view style="display: flex;flex-direction: row;align-items: center;">
						<view style="font-weight: 700;color: #FF4850;font-size: 20px;margin-bottom: 8upx;"><text
								style="font-size: 10px;">
								￥
							</text>{{item.price}}</view>
						<view style="color: #A0A0A0;font-size: 20upx;margin-left: 15upx;text-decoration: line-through">
							{{item.originPrice}}积分
						</view>
					</view>
					<view style="display: flex;flex-direction: row;;align-items: center;
					justify-content: space-between;margin-bottom: 10upx;">
						<view style="display: flex;flex-direction: row;align-items: center;">
							<view style="background-color: #252526;color: #EDCF87;
							font-size: 18upx;width: 60upx;height: 32upx;line-height: 32upx;
							text-align: center;border-bottom-left-radius: 16upx;border-top-left-radius: 16upx;">付费</view>
							<view style="background-color: #EDCF87;color: #252526;
							font-size: 18upx;height: 32upx;line-height: 32upx;
							text-align: center;border-bottom-right-radius: 16upx;border-top-right-radius: 16upx;padding: 0 5upx;">小程序</view>
						</view>
						<view style="font-size: 25upx;color: #A6A6A6;margin-right: 40upx;">{{item.viewCount}}人浏览</view>
					</view>
				</view>
			</view>
		</view>
		<view v-show="isEmpty" style="margin-top: 50%;">

			<u-empty mode="search" icon="http://cdn.uviewui.com/uview/empty/car.png">
			</u-empty>
		</view>
		
		<view style="margin-top: 20upx;" v-show="!isOperatePull">
			<view class="f-header m-t">
				<image src="/static/temp/h1.png"></image>
				<view class="tit-box">
					<text class="tit" style="font-weight: bold;">热门源码</text>
				</view>
				<!-- <text class="yticon icon-you" @click="toCategory(1)"></text> -->
			</view>
			<water :list="flowList"></water>
			
			
		</view>
		
		
		
		




		<view class="popup" v-show="show">
			<view class="popup-info">
				<view style="display: flex;flex-direction: column;align-items: center;">
					<view style="color: #191919;font-size: 32upx;margin-top: 48upx;">是否确认清空内容？</view>
				</view>

				<view
					style="display: flex;flex-direction: row;align-items: center;justify-content: space-between;margin-top: 56upx;">
					<view style="background: #F4F4F4;color: #191919;font-size: 28upx;width: 240upx;height: 80upx;margin-left: 30upx;
							border-radius: 32upx;text-align: center;line-height: 80upx;" @tap="cancel">取消</view>
					<view style="background: #F8D55D;color: #FFFFFF;font-size: 28upx;width: 240upx;
							height: 80upx;margin-right: 30upx;border-radius: 32upx;text-align: center;line-height: 80upx;" @tap="confrim">确认
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// import {create_search_content,search_list} from '@/api/search_content.js'
	import water from '../../components/water.vue';
	import {
		resource_code_search,resource_code_recommend_list
	} from '../../api/resource_code.js'
	import {
		history_search_list,
		history_search_remove
	} from '../../api/history_search.js'
	import {
		hot_search_list
	} from '../../api/config.js'
	export default {
		components: {
			water
		},
		data() {
			return {
				// background: {
				// 	'background-image': 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
				// },
				keyword: '',
				searchIconStyle: {
					'display': 'none'
				},
				hotList: [],
				actionStyle: {
					'color': '#fff'
				},
				searchContentList: [],
				resourceList: [],
				flowList: [],
				page: 1,
				current: 1,
				is_loading: true,
				isOperatePull: false,
				show: false,
				isEmpty: false,
				// top
				scrollTop: 0,
				icon: 'arrow-up',

				iconStyle: {
					color: '#ffffff',
					fontSize: '30rpx'
				},
				tips: '顶部',
				customStyle: {
					backgroundColor: '#6BC362',
					color: '#ffffff'
				},
			}
		},
		onLoad() {
			this.isOperatePull=false
			this.getHostSearchList();
			this.getHistorySearchList()
			this.getHotResourceCodeList()
		},
		onShow() {
			this.getHostSearchList();
			this.getHistorySearchList()
			// this.getHotResourceCodeList()
		},

		methods: {
			getHotResourceCodeList(){
				resource_code_recommend_list({}).then(res=>{
					this.flowList=res.data
				})
			},
			
			getHostSearchList() {
				hot_search_list({}).then(res => {
					console.log(res)
					this.hotList = res.data
				})
			},
			removeHistory() {
				history_search_remove({}).then(res => {
					uni.showToast({
						title: '操作成功',
						icon: 'none'
					})
					this.show = false
					this.getHistorySearchList()
				})
			},
			cancel() {
				this.show = false;
			},
			// 点击弹窗确认
			confrim() {
				// 清空搜索内容
				this.removeHistory()
			},
			getHistorySearchList() {
				history_search_list({}).then(res => {
					this.searchContentList = res.data
				})
			},
			clickTagHot(val) {
				this.isOperatePull=true
				let params = {}
				this.current = 1;
				this.isShowHotList=false
				this.resourceList = []
				params.pageSize = 10;
				this.keyword = val;
				params.keyword = val;
				resource_code_search(params).then(res => {
					console.log(res)
					if (res.data.length > 0) {
						this.resourceList = res.data
						this.isEmpty = false
					} else {
						this.isEmpty = true
					}

				})
			},
			clickTag(val) {
				let params = {}
				this.isOperatePull=true
				this.resourceList = []
				this.isShowHotList=false
				params.pageSize = 10;
				this.keyword = val;
				params.keyword = val;
				resource_code_search(params).then(res => {
					console.log(res)
					if (res.data.length > 0) {
						this.resourceList = res.data
					} else {
						this.isEmpty = true
					}

				})
			},
			toDetail(id) {
				uni.navigateTo({
					url: `/pages/product/product?id=${id}`
				})
			},
			deleteAll() {
				this.show = true
			},

			getResourcePage() {
				let params = {}
				this.isShowHotList=false
				this.resourceList = []
				params.pageSize = 10;
				params.keyword = this.keyword;
				resource_code_search(params).then(res => {
					console.log(res)
					if (res.data.length > 0) {
						this.resourceList = res.data
					} else {
						this.isEmpty = true
					}

				})
			},

			searchList() {
				let params = {}
				search_list({}).then(res => {
					console.log(res)
					this.searchContentList = res.data

				})
			},

			onReachBottom() {
				if(this.isOperatePull){
					let params = {}
					params.current = this.current + 1
					params.pageSize = 10;
					this.isShowHotList=false
					params.keyword = this.keyword;
					resource_code_search(params).then(res => {
						this.current = this.current + 1
						console.log(res.data)
						if (res.data.length !== 0 ) {
							this.resourceList = this.resourceList.concat(res.data)
							this.isEmpty = false
						} else {
							// this.isEmpty = true
						}
					
					})
				}
				
			},
			delhistory() {
				this.$common.successToShow('清除成功');
			},
			// top
			onPageScroll(e) {
				this.scrollTop = e.scrollTop;
			},
			custom(value) {
				this.isOperatePull=true
				this.resourceList = []
				console.log(value)
				if (value === null || value === '') {
					this.resourceList = []
					uni.showToast({
						title: '请输入搜索内容',
						icon: 'none'
					})
					return
				}
				console.log(value)
				let params = {}
				params.pageSize = 10;
				params.keyword = value;
				resource_code_search(params).then(res => {
					console.log(res)
					if (res.data.length > 0) {
						this.resourceList = res.data
					} else {
						this.isEmpty = true
					}

				})
			},

			search(value) {
				this.isOperatePull=true
				this.resourceList = []
				console.log(value)
				if (value === null || value === '') {
					this.resourceList = []
					uni.showToast({
						title: '请输入搜索内容',
						icon: 'none'
					})
					return
				}
				let params = {}

				params.pageSize = 10;
				params.keyword = value;
				resource_code_search(params).then(res => {
					console.log(res)
					if (res.data.length > 0) {
						this.resourceList = res.data
					} else {
						this.isEmpty = true
					}

				})
			},

			change(value) {
				if (value !== null || value !== '') {
					this.resourceList = []
				}

			},
		}
	}
</script>

<style lang="scss" scoped>
	
	.f-header {
		display: flex;
		align-items: center;
		height: 140rpx;
		padding: 6rpx 30rpx 8rpx;
		background: #fff;
	
		image {
			flex-shrink: 0;
			width: 80rpx;
			height: 80rpx;
			margin-right: 20rpx;
		}
	
		.tit-box {
			flex: 1;
			display: flex;
			flex-direction: column;
		}
	
		.tit {
			font-size: $font-lg +2rpx;
			color: #font-color-dark;
			line-height: 1.3;
		}
	
		.tit2 {
			font-size: $font-sm;
			color: $font-color-light;
		}
	
		.icon-you {
			font-size: $font-lg +2rpx;
			color: $font-color-light;
		}
	}
	
	.popup-info {
		position: fixed;
		width: 600upx;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		padding: 40upx;
		border-radius: 20upx;
		background-color: #fff;
		z-index: 9999;
	}

	.popup {
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		height: 100vh;
		background-color: rgba(0, 0, 0, 0.6);
		z-index: 9998;
	}

	.background {
		background-image: linear-gradient(45deg, rgb(248, 213, 93), rgb(248, 213, 93));
	}

	.tag {
		margin-right: 20rpx;
		display: inline-block;
		margin-bottom: 20rpx;
	}

	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}

	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.demo-img-wrap {}

	.demo-image {
		width: 100%;
		border-radius: 4px;
	}

	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
		word-break: break-all;
		font-weight: 700;
	}

	.demo-tag {
		display: flex;
		flex-wrap: wrap;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #ffffff;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 18rpx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1px solid $u-type-success;
		color: $u-type-success;
		margin-right: 10rpx;
		margin-bottom: 10rpx;
		border-radius: 50rpx;
		line-height: 1;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}

	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}

	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
	}
</style>
